package com.oa.works.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.oa.works.pojo.vo.ExEclOutputVO;
import com.oa.works.dao.EmailDao;
import com.oa.works.dao.EmailHospitalRelationDao;
import com.oa.works.dao.ScDao;
import com.oa.works.pojo.Sc;
import com.oa.works.pojo.vo.EmailHospitalRelationVO;
import com.oa.works.pojo.vo.EmailVO;
import com.oa.works.service.ExeclOutputService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ExeclOutputServiceImpl implements ExeclOutputService {

    @Autowired
    ScDao scDao;
    @Autowired
    EmailDao emailDao;
    @Autowired
    EmailHospitalRelationDao emailHospitalRelationDao;

    @Override
    public void execlOutput(HttpServletResponse response, ExEclOutputVO dto){
        List<Sc> dataBaseList = scDao.selectDate();
        List<ExEclOutputVO> exportList = new ArrayList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

        for (Sc data : dataBaseList) {
            ExEclOutputVO exEclOutputVO = new ExEclOutputVO();
            BeanUtils.copyProperties(data, exEclOutputVO);
            if (null != data.getSid()){
                Integer i = 1;
                exEclOutputVO.setSID(i);
            }
            if (null != data.getCid()){
                String str1 = "0";
                exEclOutputVO.setCID(str1);
            }
            if (null !=data.getScore()){
                String str2 = "0";
                exEclOutputVO.setCID(str2);
            }
            exportList.add(exEclOutputVO);
        }
        try {
            EasyExcel.write(response.getOutputStream(), ExEclOutputVO.class).sheet().doWrite(exportList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String execlOutputTest1(String filename) {
        String code = "0";

        //创建写入对象
        ExcelWriter excelWriter = EasyExcel.write(filename).build();
        for (int i1 = 3;i1>-1;i1--) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
            SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH,-i1);
            String sheetName = sdf.format(calendar.getTime())+"发送日期汇总";
            //根据时间设置查询的参数
            String submitNo = df.format(calendar.getTime());

            /* 将要展示的数据放入list中 */
            List<EmailVO> dataBaseList1 = emailDao.selectBySubmitID(submitNo);
            List<EmailVO> dataBaseList3 = new ArrayList<>();

            /* 插入病例单数 */
            for (int i = 0; i < dataBaseList1.size(); i++) {
                EmailVO emailVO = dataBaseList1.get(i);
                if (StringUtils.isBlank(emailVO.getCaseNo())) {
                    Integer s = 0;
                    emailVO.setNumber(s);
                    dataBaseList3.add(emailVO);
                } else {
                    String caseNo = emailVO.getCaseNo();
                    Integer n = caseNo.length() - caseNo.replaceAll(",", "").length();
                    Integer m = n + 1;
                    emailVO.setNumber(m);
                    dataBaseList3.add(emailVO);
                }
            }

            /* 将要展示的数据放入list中 */
            List<EmailHospitalRelationVO> dataBaseList2 = emailHospitalRelationDao.selectBySubmitNo(submitNo);

            /* 将数据写入execl文件中 */
            try {

                //设置sheet
                WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build();
                //设置table1
                WriteTable writeTable0 = EasyExcel.writerTable(0).head(EmailVO.class).needHead(true).build();
                //设置table2
                WriteTable writeTable1 = EasyExcel.writerTable(1).head(EmailHospitalRelationVO.class).needHead(true).build();
                excelWriter.write(dataBaseList1, writeSheet, writeTable0);
                excelWriter.write(dataBaseList2, writeSheet, writeTable1);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 关闭流
        excelWriter.finish();
        return code;
    }
}



